package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.param.CategoryAddNewParam;
import cn.tedu.csmall.product.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryTreeItemVO;
import cn.tedu.csmall.product.service.ICategoryService;
import com.example.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/categories")
@Api(tags = "05.类别管理模块")
@Slf4j
@Validated
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @PostMapping("/add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid CategoryAddNewParam categoryAddNewParam) {
        log.warn("开始处理【添加类别】,参数:{}",categoryAddNewParam);
        categoryService.addNew(categoryAddNewParam);

        return JsonResult.ok();
    }
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")})
    public JsonResult delete(@Range(min = 1, message = "根据ID删除类别失败,请提交合规的ID值")
                          @PathVariable Long id){
        log.warn("开始处理【根据ID删除类别】,参数:{}", id);
        categoryService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/id/update
    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/pms/category/update')")
    @ApiOperation("修改类别详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> update(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id,
                                           @Valid CategoryUpdateInfoParam categoryUpdateInfoParam) {
        log.debug("开始处理【修改类别详情】的请求，ID：{}，新数据：{}", id, categoryUpdateInfoParam);
        categoryService.update(id, categoryUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/id/enable
    @PostMapping("/{id:[0-9]+}/enable")
    @PreAuthorize("hasAuthority('/pms/category/update')")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【启用类别】的请求，参数：{}", id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/id/disable
    @PostMapping("/{id:[0-9]+}/disable")
    @PreAuthorize("hasAuthority('/pms/category/update')")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【禁用类别】的请求，参数：{}", id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/id/display
    @PostMapping("/{id:[0-9]+}/display")
    @PreAuthorize("hasAuthority('/pms/category/update')")
    @ApiOperation("显示类别")
    @ApiOperationSupport(order = 312)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setDisplay(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【显示类别】的请求，参数：{}", id);
        categoryService.setDisplay(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/id/hidden
    @PostMapping("/{id:[0-9]+}/hidden")
    @PreAuthorize("hasAuthority('/pms/category/update')")
    @ApiOperation("隐藏类别")
    @ApiOperationSupport(order = 313)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setHidden(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【隐藏类别】的请求，参数：{}", id);
        categoryService.setHidden(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/id
    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/pms/category/read')")
    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<CategoryStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【根据ID查询类别详情】的请求，参数：{}", id);
        CategoryStandardVO queryResult = categoryService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9180/categories/tree
    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('/pms/category/read')")
    @ApiOperation("查询类别树")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<CategoryTreeItemVO>> listTree() {
        log.debug("开始处理【获取类别树】的业务，参数：无");
        List<CategoryTreeItemVO> categoryTree = categoryService.listTree();
        return JsonResult.ok(categoryTree);
    }

    @GetMapping("/list-by-parent")
    @PreAuthorize("hasAuthority('/pms/category/read')")
    @ApiOperation("根据父类ID查询类别列表")
    @ApiOperationSupport(order = 421)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父类ID", paramType = "query")
    })
    public JsonResult list(@Range(min = 0, message = "查询类别列表失败，请提供正确的页码值！")
                                       Long parentId) {
        log.debug("开始处理【查询类别列表】的请求，参数：{}", parentId);
        if (parentId == null || parentId < 0L) {
            parentId = 0L;
        }
        List<CategoryListItemVO> categoryListItemVOList = categoryService.listByParentId(parentId);
        return JsonResult.ok(categoryListItemVOList);
    }


}
